It is a security vulnerability to call printf
with a unique string argument that is not a string literal. Indeed, if this argument
comes from a user input, this user can:
- make the program crash by executing code equivalent to:
printf("%s%s%s%s%s%s%s%s")
- view the stack or memory at any location by executing code equivalent to:
printf("%08x %08x %08x %08x %08x\n")
Starting with C++23, std::print
should be preferred: its arguments are validated at compile-time, making it more secure.
Noncompliant code example
void f(char* userInput) {
printf(userInput); // Noncompliant
}
Compliant solution
void f(char* userInput) {
printf("%s", userInput); // Compliant
}